<?php
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/config/city_config.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");

error_reporting(0);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival["p"], "p", "");
	$g_cgival["p"] = intval($g_cgival["p"]);
	
	if($g_cgival["p"] < 1) $g_cgival["p"]=1;
	if($g_cgival["p"] > 50) $g_cgival["p"]=50;
	cgi::both($g_cgival["pcount"], "pcount", 20);
	$g_cgival["pcount"] = intval($g_cgival["pcount"]);
	
	cgi::both($g_cgival["province_id"], "province_id", "");
	$g_cgival["province_id"] = intval($g_cgival["province_id"]);
	
	cgi::both($g_cgival["city_id"], "city_id", "");
	$g_cgival["city_id"] = intval($g_cgival["city_id"]);
	
	cgi::both($g_cgival["topic_id"], "topic_id", "");
	$g_cgival["topic_id"] = intval($g_cgival["topic_id"]);
	
	cgi::both($g_cgival["verified_type"], "verified_type", "");
	$g_cgival["verified_type"] = string::un_html($g_cgival["verified_type"]);
	
	cgi::both($g_cgival["key"], "key", "");
	$g_cgival["key"] = string::un_html($g_cgival["key"]);

	cgi::both($g_cgival["age"], "age", "");
	$g_cgival["age"] = string::un_html($g_cgival["age"]);
	
	
	cgi::both($g_cgival["sex"], "sex", "");
	$g_cgival["sex"] = string::un_html($g_cgival["sex"]);
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["is_logined"] = true;
		$g_show["current_user_info"] = $user_detail_info;
	}
	
	$province_id = $g_cgival["province_id"];
	$city_id = $g_cgival["city_id"];
	$topic_id = $g_cgival["topic_id"];
	$search_key = $g_cgival["key"] ;
	$sex = $g_cgival["sex"] ;
	$age = $g_cgival["age"] ;
	
	$verified_type = $g_cgival["verified_type"] ;
	
	
	$db_user_search = new db_user_search();
	$db_user_info = new db_user_info();
	
	if($age)
	{
		$birthyear[0] = date("Y")- $age -3;
		$birthyear[1] = date("Y")- $age + 3;
	}
	
	
	
	if($topic_id)
	{
				
		$count = $db_user_search->get_user_count_by_search($search_key,$province_id,$city_id,$topic_id,$sex,$verified_type,"","",$birthyear);
		$list = $db_user_search->get_user_list_by_search($g_cgival["p"],$g_cgival["pcount"],$search_key,$province_id,$city_id,$topic_id,$sex,$verified_type,"","","",$birthyear);
		
		$uids = array();
		foreach($list as $key => $value)
		{
			$uids[] = $value["uid"];
		}
		$list = $db_user_info->get_user_list_by_uids($uids);
	}
	else
	{
		$count = $db_user_info->get_user_count_by_search($search_key,$province_id,$city_id,$sex,$verified_type,$school_id="",$start_year="",$birthyear);
		$list = $db_user_info->get_user_list_by_search($g_cgival["p"],$g_cgival["pcount"],$search_key,$province_id,$city_id,$sex,$verified_type,$school_id="",$start_year="",
		$not_in_ids="",$birthyear);
		
	}
	
	
	$uids = array();
	foreach($list as $key => $value)
	{
	
		$uids[] = $value["uid"];
	
	}
	
	$uid_array = array_chunk($uids,20);
	$sina_user_info =array();
	
	$mblog_cache_v2 = new mblog_cache_v2();
	foreach($uid_array as $uid_item)
	{
		$uids = join(",",$uid_item);
		$user_list = $mblog_cache_v2->cache_api_get_user_info_by_uids($uids,$screen_name="",$trim_status=1,$has_extend=1);
		
		foreach($user_list['data']["result"] as $key => $value)
		{
			$sina_user_info["{$value["id"]}"] = $value;
		}
	}
	
	foreach($list as $key => $value)
	{
		$list[$key]["location"] = $config["province"][$value["province"]]["name"]." ".$config['city'][$value["province"]][$value["city"]];
		$list[$key]["detail_info"] = $sina_user_info["{$value["uid"]}"];
		$list[$key]["detail_info"]["big_header"] = str_replace("/50/","/180/",$list[$key]["detail_info"]["profile_image_url"]);
	}

	$g_show["list"] = $list;

	
	
	$g_show["count"] = $count;
	
	
	if($g_show["count"] > 500) $g_show["count"] =500;
	// 分页
	$page_count = page_bar::get_page_max($g_show["count"], $g_cgival["pcount"],$g_cgival["p"]);
	
	$page_bar_style = array(
		"curt"		=> $g_cgival["p"],
		"max"		=> $page_count,
		"key"		=> "p",
		);
		
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$page_count}页&nbsp;" . page_bar::bar2($page_bar_style);
	
	
 	
 	
 	
	$g_show["page_keywords"]  = "用户搜索,游戏专辑分享,游戏分享,微博玩家,新浪微博玩家,游戏分类专辑,游戏专辑图片";
	$g_show["page_desc"]  = "用户搜索,分享微博图片专辑,新浪微博玩家发的游戏图片专辑,根据游戏分类推荐专辑";
	$g_show["page_title"] ="用户搜索";
 	
 	
 	
 	#############省配置
 	$province = array();
 	foreach($config['province'] as $key => $value)
 	{
 		$province[] = array("id"=>$key,"name"=>$value["name"]);
 	}
 
 	$g_show["province"] = $province;
 	
	#############市配置
	$city_list= array();
	
	if($province_id)
	{
		foreach($config['city'][$province_id] as $key => $value)
		{
			$city_list[] = array("id"=>$key,"name"=>$value);
		}
		$g_show["city_list"] = $city_list;
	}
	
 	
 	
 	
 	#############游戏话题配置
 	$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
 	$topic_list = $db_mblog_cron_topic_tag->get_mblog_topic_list_need_cron(1,20,array(1,2,3));
 	$g_show["topic_list"] = $topic_list;
 	$g_show["province_id"] = $province_id;//$user_detail_info["province"];
 	$g_show["city_id"] = $city_id;//$user_detail_info["city"];
 	$g_show["key"] =$search_key;
 
 	$g_show["sex"] =$sex;
 	$g_show["topic_id"] =$topic_id;
 	$g_show["verified_type"] =$verified_type;
 	
 	
 	$mod_mblog = new mod_mblog();
	list($list1,$list2,$list3,$list4) = $mod_mblog->get_topic_list_order_by_like();
	$g_show["list1"] = $list1;
	$g_show["list2"] = $list2;
	$g_show["list3"] = $list3;
	$g_show["list4"] = $list4;
	
	
	$Pages = new Pages($g_show["count"],$g_cgival["p"],$g_cgival["pcount"]);
	$g_show["page_bar"] = $Pages->getLinks("?key={$g_cgival["key"]}&topic_id={$g_cgival["topic_id"]}&sex={$g_cgival["sex"]}&provice_id={$g_cgival["provice_id"]}&city_id={$g_cgival["city_id"]}&verified_type={$g_cgival["verified_type"]}&age={$g_cgival["age"]}&p=",8);
	
}

try {
	$g_show['error'] = 0;
	$g_show['errmsg'] = "";
	check_cgi_pro();
	get_data();
	$g_show["hide_shadow"] = true;
	
} catch (Exception $e) {
	$g_show['error'] = 1;
	$g_show['errmsg'] = $e->getMessage();
}

show_pro();


exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	$smarty = p_get_smarty();	
	$smarty->assign("g_show", $g_show);
	if (isset($_GET["debug"]))
	{
		print_r($g_show);
	}
	$smarty->display("search/huaban_user.html");
	
	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
	
}

